package models

import (
	"rongyy/src/app"
	"rongyy/src/middlewares"
)

type YktModel struct {
	BaseModel  `xorm:"extends"`
	PayOrderId string `json:"pay_order_id"`
	MchOrderId string `json:"mch_order_id"`
	CardNo     string `json:"card_no"`
	IsUsed     int    `json:"is_used"`
}

type YktPayApiReqModel struct {
	IdNameOrder     string `json:"idNameOrder"`
	IdNumberOrder   string `json:"idNumberOrder"`
	IdTypeOrder     string `json:"idTypeOrder"`
	MchOrderId      string `json:"mchOrderId"`
	NotifyUrl       string `json:"notifyUrl"`
	OrderAmount     int    `json:"orderAmount"`
	OrderCreateTime string `json:"orderCreateTime"`
	OrderDesc       string `json:"orderDesc"`
	OrderTitle      string `json:"orderTitle"`
	OrderType       string `json:"orderType"`
	PayType         string `json:"payType"`
	WayCode         string `json:"wayCode"`
	TransactionId   string `json:"transaction_id"`
	PatientId       string `json:"patient_id"`
	Openid          string `json:"openid"`
}

type YktPayReqModel struct {
	IdNameOrder     string  `json:"idNameOrder"`
	IdNumberOrder   string  `json:"idNumberOrder"`
	IdTypeOrder     string  `json:"idTypeOrder"`
	MchOrderId      string  `json:"mchOrderId"`
	NotifyUrl       string  `json:"notifyUrl"`
	OrderAmount     float64 `json:"orderAmount"`
	OrderCreateTime string  `json:"orderCreateTime"`
	OrderDesc       string  `json:"orderDesc"`
	OrderTitle      string  `json:"orderTitle"`
	OrderType       string  `json:"orderType"`
	PayType         string  `json:"payType"`
	WayCode         string  `json:"wayCode"`
}

//type YktPayReqModel struct {
//	PayOrderId string `json:"payOrderId"`
//	MchOrderId string `json:"mchOrderId"`
//}

type YktQueryOrderReqModel struct {
	MchOrderId string `json:"mchOrderId"`
	PayOrderId string `json:"payOrderId"`
}

type YktQueryOrderRespModel struct {
	ChannelMsg     string `json:"channelMsg"`
	ChannelOrderId string `json:"channelOrderId"`
	CouponAmount   int    `json:"couponAmount"`
	MchOrderId     string `json:"mchOrderId"`
	OrderAmount    int    `json:"orderAmount"`
	PayOrderId     string `json:"payOrderId"`
	PayStatus      string `json:"payStatus"`
	PayTime        string `json:"payTime"`
	ReceiptAmount  int    `json:"receiptAmount"`
	TotalAmount    int    `json:"totalAmount"`
	WayCode        string `json:"wayCode"`
}

type YktRespModel struct {
	Code    string           `json:"code"`
	Msg     string           `json:"msg"`
	SubCode string           `json:"sub_code"`
	SubMsg  string           `json:"sub_msg"`
	BizData YktRespDataModel `json:"biz_data"`
}

type YktRespDataModel struct {
	Aaz001  string `json:"aaz001"`
	Appid   string `json:"appid"`
	Code    string `json:"code"`
	Data    string `json:"data"`
	Status  string `json:"status"`
	Success bool   `json:"success"`
	Message string `json:"message"`
}

type YktPayRespModel struct {
	MchOrderId string `json:"mchOrderId"`
	PayOrderId string `json:"payOrderId"`
	PayUrl     string `json:"payUrl"`
}

type YktSm2aEncryptoReq struct {
	Key     string `json:"key"`
	Content string `json:"content"`
}

type YktSm2EncryptoReq struct {
	Key     string `json:"key"`
	Content string `json:"content"`
}

type YktSm4DecryptoReq struct {
	Key            string `json:"key"`
	EncryptContent string `json:"encryptContent"`
}

type YktPayReq struct {
	Appid  string `json:"appid"`
	Aaz001 string `json:"aaz001"`
	Data   string `json:"data"`
}

type YktPayFinalResp struct {
	MchOrderId string `json:"mchOrderId"`
	PayOrderId string `json:"payOrderId"`
	PayUrl     string `json:"payUrl"`
}
type YktRefundResp struct {
	ChannelMsg           string `json:"channelMsg"`
	ChannelRefundOrderId string `json:"channelRefundOrderId"`
	MchOrderId           string `json:"mchOrderId"`
	MchRefundId          string `json:"mchRefundId"`
	PayAmount            int    `json:"payAmount"`
	PayOrderId           string `json:"payOrderId"`
	RefundAmount         int    `json:"refundAmount"`
	RefundOrderId        string `json:"refundOrderId"`
	RefundRelAmt         int    `json:"refundRelAmt"`
	RefundStatus         string `json:"refundStatus"`
	RefundTime           string `json:"refundTime"`
}

type YktPayFinalPayUrlResp struct {
	Path      string `json:"path"`
	ExtraData struct {
		PayNo string `json:"payNo"`
	} `json:"extraData"`
	Appid string `json:"appid"`
	Type  string `json:"type"`
	Url   string `json:"url"`
}
type YktPayDeResp struct {
	Path      string `json:"path"`
	ExtraData struct {
		PayNo string `json:"payNo"`
	} `json:"extraData"`
	Appid string `json:"appid"`
	Type  string `json:"type"`
	Url   string `json:"url"`
}

// 订单查询出参
type YktQueryOrderData struct {
	ChannelMsg     string `json:"channelMsg"`
	ChannelOrderId string `json:"channelOrderId"`
	CouponAmount   int    `json:"couponAmount"`
	MchOrderId     string `json:"mchOrderId"`
	OrderAmount    int    `json:"orderAmount"`
	PayOrderId     string `json:"payOrderId"`
	PayStatus      string `json:"payStatus"`
	PayTime        string `json:"payTime"`
	ReceiptAmount  int    `json:"receiptAmount"`
	TotalAmount    int    `json:"totalAmount"`
	WayCode        string `json:"wayCode"`
}

// 退费
type YktRefundReq struct {
	MchOrderId   string  `json:"mchOrderId"`
	PayOrderId   string  `json:"payOrderId"`
	RefundAmount float64 `json:"refundAmount"`
	MchRefundId  string  `json:"mchRefundId"`
	RefundReason string  `json:"refundReason"`
}

func (pm *YktModel) TableName() string {
	return "ykt_record"
}

func (rm *YktModel) Insert() (*YktModel, error) {
	_, err := app.DB().Insert(rm)
	if err != nil {
		middlewares.Logger.Println("insert user fail", err)
		return nil, err
	}
	return rm, err
}

func (rm *YktModel) Get(cardNo string) ([]YktModel, int64, error) {
	cms := make([]YktModel, 0)
	s := app.DB().Table("ykt_record").Where("card_no=? and is_used=0", cardNo)
	total, err := s.Limit(999, 0).Limit(1).Desc("created_at").FindAndCount(&cms)
	if err != nil {
		return nil, 0, err
	}
	return cms, total, err
}

func (rm *YktModel) Used() (*YktModel, error) {
	rm.IsUsed = 1
	_, err := app.DB().Where("pay_order_id =? and card_no=?", rm.PayOrderId, rm.CardNo).Cols("is_used").Update(rm)
	if err != nil {
		middlewares.Logger.Println("insert user fail", err)
		return nil, err
	}
	return rm, err
}

func (rm *YktModel) Query(cardNo string) ([]YktModel, int64, error) {
	cms := make([]YktModel, 0)
	s := app.DB().Table("ykt_record").Where("card_no=? and is_used=0", cardNo)
	total, err := s.Limit(999, 0).Limit(1).Desc("created_at").FindAndCount(&cms)
	if err != nil {
		return nil, 0, err
	}
	return cms, total, err
}
